# ###############################################
# Detect if we are targeting iOS.  If so, validate
# other settings.
IF( TARGET_IOS AND COMPILER_CLANG )

	# Override this platform and tell CMake to cross compile to iOS.
	SET( CMAKE_CROSSCOMPILING TRUE )
	SET( CMAKE_SYSTEM_NAME "Darwin" )
	SET( CMAKE_SYSTEM_PROCESSOR "arm" )

	# Setup the SDK selection.
	SET( SDKVER "6.1" )
	SET( DEVROOT "/Applications/Xcode.app/Contents/Developer/Platforms/iPhoneOS.platform/Developer" )
	SET( SDKROOT "${DEVROOT}/SDKs/iPhoneOS${SDKVER}.sdk" )
	IF( EXISTS ${SDKROOT} )
		SET( CMAKE_OSX_SYSROOT "${SDKROOT}" )
	ELSE()
		MESSAGE( "Warning, iOS Base SDK path not found: " ${SDKROOT} )
	ENDIF()

	# Setup the valid architectures.
	SET( CMAKE_OSX_ARCHITECTURES "$(ARCHS_STANDARD_32_BIT)" )

	# Allow simulator and deploy.
	SET( CMAKE_XCODE_EFFECTIVE_PLATFORMS "-iphoneos;-iphonesimulator" )

	# Unit tests must be disabled on IOS, console applications are not
	# supported.  A very simple Cocoa Touch app could be written to
	# run and display the test results though.
	SET( BUILD_UNIT_TESTS OFF )

	# Enable NEON.  NOTE: Only available to devices, will have to compile
	# out if targetting a simulator.
	IF( SIMD_NEON )
		SET( CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -mfloat-abi=softfp -mfpu=neon" )
		SET( CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -mfloat-abi=softfp -mfpu=neon" )
	ENDIF()

# ###############################################
# Detect GNU or Clang compilers.
ELSEIF( COMPILER_GNU OR COMPILER_CLANG )

	IF( SIMD_SSE_AVX )
		SET( CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -mavx" )
		SET( CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -mavx" )

	ELSEIF( SIMD_SSE_4 )
		SET( CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -msse4.2" )
		SET( CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -msse4.2" )

	ELSEIF( SIMD_SSE_3_1 )
		SET( CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -mssse3" )
		SET( CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -mssse3" )

	ELSEIF( SIMD_SSE_3 )
		SET( CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -msse3" )
		SET( CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -msse3" )

	ELSEIF( SIMD_SSE_2 )
		SET( CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -msse2" )
		SET( CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -msse2" )

	ELSEIF( SIMD_SSE )
		SET( CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -msse" )
		SET( CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -msse" )

	ENDIF()

ENDIF()


# ###############################################
# Enable C++11 on Apple machines.
IF( COMPILER_CLANG )
	SET( CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -std=c++11 -stdlib=libc++" )
	SET( CMAKE_EXE_LINKER_FLAGS "${CMAKE_EXE_LINKER_FLAGS} -stdlib=libc++" )
	SET( CMAKE_SHARED_LINKER_FLAGS "${CMAKE_SHARED_LINKER_FLAGS} -stdlib=libc++" )
	SET( CMAKE_MODULE_LINKER_FLAGS "${CMAKE_MODULE_LINKER_FLAGS} -stdlib=libc++" )

	# Tell Xcode that we really do mean for it to recognize C++11 and libc++ properly.
	IF( CMAKE_GENERATOR STREQUAL Xcode )
		SET( CMAKE_XCODE_ATTRIBUTE_CLANG_CXX_LANGUAGE_STANDARD "c++11" )
		SET( CMAKE_XCODE_ATTRIBUTE_CLANG_CXX_LIBRARY "libc++" )
	ENDIF()
ENDIF()

# ###############################################
# Fix Google Mock to work with Clang on OsX.
# This is a big, yet safe, cheat.
IF( APPLE AND COMPILER_CLANG )
	INCLUDE_DIRECTORIES( External/CMake/GMock )
ENDIF()

